﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jy123.BLL.Public;

namespace Jy123.BLL.Guess
{
    public class GuessInfo
    {
        private State_Record BLLState = new State_Record();
        private Model.State_Record record = new Jy123.Model.State_Record();

        /// <summary>
        /// 获取竞猜项统计
        /// </summary>
        /// <param name="guessID">竞猜项ID</param>
        /// <returns>竞猜份数</returns>
        public string getGuessItemStat(int FItem)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.AppendFormat("select count(1) from user_guess_stat where fitem = {0}", FItem);

            return DBUtility.DbHelperSQL.GetSingle(strSql.ToString()).ToString();
        }

        /// <summary>
        /// 获取用户竞猜信息实体类
        /// </summary>
        /// <param name="FUser">用户ID</param>
        /// <param name="FGuess">竞猜用户ID</param>
        /// <returns>User_StatInfo实体类</returns>
        public Jy123.Model.User_StatInfo getUserGuesssStat(int FUser, int FGuess)
        {
            Jy123.Model.User_StatInfo statinfo = new Jy123.Model.User_StatInfo();

            statinfo.Count = getGuessCount(FUser, FGuess);//参与竞猜次数

            int maxcount = getGuessShare(FGuess);//最大竞猜次数

            if (statinfo.Count == maxcount)
            {
                statinfo.IsMax = true;
            }
            else
            {
                statinfo.IsMax = false;
            }

            string strItem = "select Distinct fitem from user_guess_stat where fuser = " + FUser + " and fguess = " + FGuess;

            statinfo.ItemID = Convert.ToInt32(DBUtility.DbHelperSQL.GetSingle(strItem));//竞猜项ID

            string strSql = "select item from user_guess_item where id =" + statinfo.ItemID;

            statinfo.Item = DBUtility.DbHelperSQL.GetSingle(strSql).ToString();//竞猜项

            return statinfo;
        
        }

        private class userStatInfo
        {
            private int _count;        
        }
        /// <summary>
        /// 获取竞猜次数
        /// </summary>
        /// <param name="FUser">用户id</param>
        /// <param name="FGuess">竞猜id</param>
        /// <returns>竞猜次数</returns>
        public int getGuessCount(int FUser, int FGuess)
        {
            StringBuilder sbSql = new StringBuilder();

            string strSql = "select count(1) from user_guess_stat where fuser ={0} and fguess ={1}";

            sbSql.AppendFormat(strSql, FUser, FGuess);

            return Convert.ToInt32(DBUtility.DbHelperSQL.GetSingle(sbSql.ToString()));
        }

        /// <summary>
        /// 获取每人竞猜允许份数
        /// </summary>
        /// <param name="FGuess">竞猜id</param>
        /// <returns>份数</returns>
        public int getGuessShare(int FGuess)
        {
            StringBuilder sbSql = new StringBuilder();

            string strSql = "select share from user_guess_main where id ={0}";

            sbSql.AppendFormat(strSql, FGuess);

            return Convert.ToInt32(DBUtility.DbHelperSQL.GetSingle(sbSql.ToString()));
        }
        /// <summary>
        /// 判断用户在该竞猜中是否存在统计
        /// </summary>
        /// <param name="guessID">竞猜ID</param>
        /// <param name="userID">用户ID</param>
        /// <returns>true/false</returns>
        public Boolean ExistsGuessStat(int guessID,int userID)
        {
            StringBuilder sbSql = new StringBuilder();

            string strSql = "select * from user_guess_stat where fguess ={0} and fuser ={1}";

            sbSql.AppendFormat(strSql, guessID, userID);

            return DBUtility.DbHelperSQL.Exists(sbSql.ToString());
        }

        /// <summary>
        /// 获取竞猜项
        /// </summary>
        /// <param name="FItem">竞猜项ID</param>
        /// <returns>竞猜项String</returns>
        public string getItem(int FItem)
        {
            StringBuilder sbSql = new StringBuilder();

            string strSql = "select item from user_guess_item where id ={0}";

            sbSql.AppendFormat(strSql, FItem);

            return DBUtility.DbHelperSQL.GetSingle(sbSql.ToString()).ToString();
        }

        /// <summary>
        /// 是否首次竞猜
        /// </summary>
        /// <param name="statModel">竞猜统计Model</param>
        /// <returns>true/false</returns>
        public bool isFristGuess(Jy123.Model.User_Guess_Stat statModel)
        {
            StringBuilder sbSql = new StringBuilder();

            string strSql = "select top 1 id from user_guess_stat where fguess ={0} and fuser ={1} order by time";

            sbSql.AppendFormat(strSql, statModel.FGuess, statModel.FUser);

            int Frist = Convert.ToInt32(DBUtility.DbHelperSQL.GetSingle(sbSql.ToString()));

            if (statModel.ID == Frist)
            {
                return true;
            }
            else
            {
                return false;            
            }
        }

        /// <summary>
        /// 获取竞猜回复ModelList
        /// </summary>
        /// <param name="FGuess">竞猜ID</param>
        /// <returns>回复modellist</returns>
        public List<Jy123.Model.User_Guess_Review> getReviewList(int FGuess)
        {
            StringBuilder sbWhere = new StringBuilder();

            string strWhere = "fguess ={0}";

            sbWhere.AppendFormat(strWhere,FGuess);

            return new BLL.User_Guess_Review().GetModelList(sbWhere.ToString());
        }

        /// <summary>
        /// 撤销竞猜
        /// </summary>
        /// <param name="FUser">用户id</param>
        /// <param name="FGuess">竞猜id</param>
        /// <returns>影响行数</returns>
        public int StatClear(int FUser, int FGuess)
        {
            StringBuilder sbSql = new StringBuilder();

            string strSql = "delete user_guess_stat where fuser ={0} and fguess ={1}";

            sbSql.AppendFormat(strSql, FUser, FGuess);

            return Convert.ToInt32(DBUtility.DbHelperSQL.ExecuteSql(sbSql.ToString()));
        }
    }
}
